package com.food.admin.handler;



import com.food.admin.exception.MyParameterException;
import com.food.admin.exception.accountException.AccountException;
import com.food.admin.exception.foodAdminException;
import com.food.admin.exception.productsTypeException.ProductsTypeException;
import com.food.admin.exception.storeInfoException.StoreInfoException;
import com.food.admin.exception.storeRegisterException.StoreRegisterException;
import com.food.admin.model.Result;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.SignatureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    //餐品类型异常处理
    @ExceptionHandler(ProductsTypeException.class)
    public Result handleProductsException(ProductsTypeException e){
        e.printStackTrace();
        return Result.fail(e.getCode()+"",e.getMessage());
    }
    //账号异常处理
    @ExceptionHandler(AccountException.class)
    public Result accountException(foodAdminException e){
        e.printStackTrace();
        return Result.fail(e.getCode()+"",e.getMessage());
    }

    @ExceptionHandler({MalformedJwtException.class, SignatureException.class})
    public Result handleJwtException(Exception e){
        e.printStackTrace();
        return new Result("500","token无效",null);
    }



    //参数校验异常处理
    @ExceptionHandler(MyParameterException.class)
    public Result handleParameterException(MyParameterException e){
        e.printStackTrace();
        return Result.fail(e.getCode()+"",e.getMessage());
    }

    @ExceptionHandler({ExpiredJwtException.class})
    public Result handleJExpiredJwtxception(Exception e){
        e.printStackTrace();
        return new Result("500","token已过期请重新登录",null);
    }

    //商铺注册异常处理
    @ExceptionHandler(StoreRegisterException.class)
    public Result handleStoreRegisterException(StoreRegisterException e){
        e.printStackTrace();
        return Result.fail(e.getCode()+"",e.getMessage());
    }


    //商铺info异常处理
    @ExceptionHandler(StoreInfoException.class)
    public Result handleStoreInfoException(StoreInfoException e){
        e.printStackTrace();
        return Result.fail(e.getCode()+"",e.getMessage());
    }

/**
     * 兜底操作
     * @param e
     * @return*/
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        e.printStackTrace();
         return new Result("500",e.getMessage(),null);
    }
}
